<template>
  <!-- 添加或修改项目信息管理对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="框架名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="招标批次编号" prop="batchNumber">
                <el-input v-model="form.batchNumber" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="框架号" prop="code">
                <el-input v-model="form.code" placeholder="请输入项目框架号"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="ECP合同号" prop="contractCode">
                <el-input v-model="form.contractCode" placeholder="请输入ECP合同号"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="项目来源" prop="source">
                <el-select v-model="form.source" placeholder="请选择项目来源" clearable style="width: 300px">
                  <el-option
                    v-for="dict in dict.type.project_source"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="业务员" prop="salesmanIds">
                <el-select v-model="form.salesmanIds" placeholder="请选择业务员" filterable clearable multiple
                           style="width: 300px">
                  <el-option v-for="item in salesmanList"
                             :key="item.userId" :label="item.nickName" :value="item.userId"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="买方" prop="customerIds">
                <el-select v-model="form.customerIds" placeholder="请选择客户" filterable clearable multiple
                           style="width: 300px">
                  <el-option v-for="dict in dict.type.goods_customer"
                             :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="签约日期" prop="contractDate">
                <el-date-picker clearable
                                v-model="form.contractDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择签约日期">
                </el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="合同金额" prop="contractMoney">
                <el-input-number :precision="2" v-model="form.contractMoney" placeholder="请输入合同金额"/>
                元
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="中标服务费" prop="bidServiceMoney">
                <el-input-number :precision="2" v-model="form.bidServiceMoney" placeholder="请输入中标服务费"/>
                元
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getAction, addAction, getById, updateAction} from '@/api/manage';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {treeselect} from "@/api/system/dept";

export default {
  name: "ProjectForm",
  dicts: ['goods_customer', 'company', 'yes_no', 'tax_status', 'project_source'],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      salesmanList: [],
      // 表单校验
      rules: {
        source: [
          {required: true, message: "请选择项目来源", trigger: "blur"}
        ],
        code: [
          {required: true, message: "项目框架号不能为空", trigger: "blur"},
        ],
        contractCode: [
          {required: true, message: "ECP合同号不能为空", trigger: "blur"}
        ],
        customId: [
          {required: true, message: "请选择买方", trigger: "blur"}
        ],
        companyId: [
          {required: true, message: "请选择项目单位", trigger: "blur"}
        ],
        salesmanId: [
          {required: true, message: "请选择业务员", trigger: "blur"}
        ],

      },
      url: {
        getById: '/project/project/',
        add: '/project/project/add',
        edit: '/project/project/edit',
        checkCodeUnique: '/project/project/checkCodeUnique/',
        salesmanList: '/system/role/authUser/allocatedList',
      },
    };
  },
  created() {
    this.getSalesmanList();
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        name: null,
        code: null,
        buyer: null,
        bidServiceMoney: null,
        customId: null,
        customName: null,
        customSimple: null,
        companyName: null,
        contractDate: null,
        contractMoney: null,
        fundCallback: null,
        kpname: null,
        kpcode: null,
        status: "0",
        customerIds: [],
        salesmanIds: [],
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data.data;
        this.form.customerIds = response.data.customerIds;
        this.form.salesmanIds = response.data.salesmanIds;
        this.open = true;
        this.title = "修改项目信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    checkCodeUnique(rule, code, callback) {
      if (code == null) {
        callback(new Error("项目编号不能为空"));
      }
      getAction(this.url.checkCodeUnique + code, {}).then(response => {
        if (!response.data) {
          callback(new Error("项目编号已存在"));
        } else {
          callback();
        }
      });
    },

    getSalesmanList() {
      getAction(this.url.salesmanList, {"roleId": "e1c2bca31b7944e98f79008c726cf7e8"}).then(response => {
        this.salesmanList = response.rows;
      });
    }


  }
};
</script>
